<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="关联会员ID" prop="memberId">
        <el-input
          v-model="queryParams.memberId"
          placeholder="请输入关联会员ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select
          v-model="queryParams.sex"
          placeholder="请选择性别"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_USER_SEX)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="联系号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系号码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="工作" prop="job">
        <el-input
          v-model="queryParams.job"
          placeholder="请输入工作"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="代理地区" prop="proxyArea">
        <el-tree-select
          v-model="queryParams.proxyArea"
          placeholder="请选择代理地区"
          clearable
          :data="areaList"
          :props="defaultProps"
          :render-after-expand="true"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="代理类型" prop="proxyType">
        <el-select
          v-model="queryParams.proxyType"
          placeholder="请选择代理类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MEMBER_PROXY_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请年限" prop="askForYear">
        <el-input
          v-model="queryParams.askForYear"
          placeholder="请输入申请年限"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_PROXY_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="代理等级" prop="proxyLevel">
        <el-input
          v-model="queryParams.proxyLevel"
          placeholder="请输入代理等级"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="主体资质" prop="qualification">
        <el-select
          v-model="queryParams.qualification"
          placeholder="请选择主体资质"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MEMBER_PROXY_QUALIFICTION)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请人职位" prop="askerPosition">
        <el-input
          v-model="queryParams.askerPosition"
          placeholder="请输入申请人职位"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="公司地址" prop="companyAddress">
        <el-input
          v-model="queryParams.companyAddress"
          placeholder="请输入公司地址"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="法人" prop="legalPerson">
        <el-input
          v-model="queryParams.legalPerson"
          placeholder="请输入法人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="公司性质" prop="companyType">
        <el-select
          v-model="queryParams.companyType"
          placeholder="请选择公司性质"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_COMPANY_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="公司注册时间" prop="registeredTime">
        <el-date-picker
          v-model="queryParams.registeredTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="注册资金" prop="registeredCapital">
        <el-input
          v-model="queryParams.registeredCapital"
          placeholder="请输入注册资金"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="公司主营业务" prop="companyBusiness">
        <el-input
          v-model="queryParams.companyBusiness"
          placeholder="请输入公司主营业务"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="同意年限" prop="agreeYear">
        <el-input
          v-model="queryParams.agreeYear"
          placeholder="请输入同意年限"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="过期时间" prop="expireTime">
        <el-date-picker
          v-model="queryParams.expireTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['member:proxy-user:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['member:proxy-user:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="分销会员ID" align="center" prop="id" />
      <el-table-column label="关联会员ID" align="center" prop="memberId" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_USER_SEX" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="联系号码" align="center" prop="phone" />
      <el-table-column label="工作" align="center" prop="job" />
      <el-table-column label="代理地区" align="center" prop="proxyAreaName" />
      <el-table-column label="代理类型" align="center" prop="proxyType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MEMBER_PROXY_TYPE" :value="scope.row.proxyType" />
        </template>
      </el-table-column>
      <el-table-column label="申请年限" align="center" prop="askForYear" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_PROXY_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="返佣比例" align="center" prop="backRate" />
      <el-table-column label="主体资质" align="center" prop="qualification">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MEMBER_PROXY_QUALIFICTION" :value="scope.row.qualification" />
        </template>
      </el-table-column>
      <el-table-column label="申请人职位" align="center" prop="askerPosition" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="公司地址" align="center" prop="companyAddress" />
      <el-table-column label="法人" align="center" prop="legalPerson" />
      <el-table-column label="公司性质" align="center" prop="companyType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_COMPANY_TYPE" :value="scope.row.companyType" />
        </template>
      </el-table-column>
      <el-table-column
        label="公司注册时间"
        align="center"
        prop="registeredTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="注册资金" align="center" prop="registeredCapital" />
      <el-table-column label="公司主营业务" align="center" prop="companyBusiness" />
      <el-table-column label="经验" align="center" prop="experience" />
      <el-table-column label="优势" align="center" prop="advantage" />
      <el-table-column label="同意年限" align="center" prop="agreeYear" />
      <el-table-column
        label="过期时间"
        align="center"
        prop="expireTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" class="w-120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['member:proxy-user:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['member:proxy-user:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ProxyUserForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ProxyUserApi, ProxyUserVO } from '@/api/member/proxyuser'
import ProxyUserForm from './ProxyUserForm.vue'
import * as AreaApi from '@/api/system/area'
import { defaultProps } from '@/utils/tree'

/** 代理用户信息 列表 */
defineOptions({ name: 'ProxyUser' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ProxyUserVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  memberId: undefined,
  name: undefined,
  sex: undefined,
  phone: undefined,
  job: undefined,
  proxyArea: undefined,
  proxyType: undefined,
  askForYear: undefined,
  status: undefined,
  proxyLevel: undefined,
  qualification: undefined,
  askerPosition: undefined,
  companyName: undefined,
  companyAddress: undefined,
  legalPerson: undefined,
  companyType: undefined,
  registeredTime: [],
  registeredCapital: undefined,
  companyBusiness: undefined,
  experience: undefined,
  advantage: undefined,
  agreeYear: undefined,
  expireTime: [],
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const areaList = ref([]) // 地区列表

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    // 获得地区列表
    areaList.value = await AreaApi.getAreaTree()
    const data = await ProxyUserApi.getProxyUserPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}
 
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProxyUserApi.deleteProxyUser(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProxyUserApi.exportProxyUser(queryParams)
    download.excel(data, '代理用户信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>